//
//  JZHFuJinViewController.m
//  极致荟
//
//  Created by hetao on 14-5-12.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHFuJinViewController.h"
#import "JZHPersonaldetailsViewController.h"
#import "JZHHuJinTableViewCell.h"
#import "OtherUsers.h"
#import "JZHDetailsViewController.h"
@interface JZHFuJinViewController ()
{
    NSMutableArray *dataarr;
}
@end

@implementation JZHFuJinViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置左侧按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.navigationItem.title=@"附近的人";
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    if(![CLLocationManager locationServicesEnabled])
    {
        [[[UIAlertView alloc]initWithTitle:@"提示" message:@"定位服务以关闭，请打开定位服务后在试" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil] show];
    }else
    {
        [self httpData];
    }
}

- (void)leftDrawerButtonPress:(UIButton *)but
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark ---tableview代理---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return dataarr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str=@"adf";
    JZHHuJinTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if(!cell)
    {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"JZHHuJinTableViewCell" owner:self options:nil] objectAtIndex:0];
        cell.contr=self;
    }
    [cell addCellData:dataarr[indexPath.row]];
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
     [self.navigationController pushViewController:[[JZHDetailsViewController alloc]initOthUser:dataarr[indexPath.row]] animated:YES];
   
    
}

- (void)httpData
{
    NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithDouble:[User defuceUser].locationmanager.location.coordinate.latitude],@"coordX",[NSNumber numberWithDouble:[User defuceUser].locationmanager.location.coordinate.longitude],@"coordY", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"friends/getRangeUser") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSLog(@"%@",data);
            if(!dataarr)
            {
                dataarr=[[NSMutableArray alloc] init];
            }
            NSString *path=[[data objectForKey:@"data"] objectForKey:@"imgPrefix"];
            for(NSArray *arr in [[data objectForKey:@"data"] objectForKey:@"list"])
            {
                for(NSDictionary *ddata in arr)
                {
                    if([[ddata objectForKey:@"userId"] intValue]==[User defuceUser].userId)
                    {
                        continue;
                    }
                    OtherUsers *ouser=[[OtherUsers alloc] init];
                    ouser.name=[ddata objectForKey:@"Name"];
                    ouser.age=[[ddata objectForKey:@"Age"] intValue];
                    ouser.distance=[[ddata objectForKey:@"Distance"] doubleValue];
                    ouser.lastLoginTime=[NSDate dateWithTimeIntervalSince1970:[[ddata objectForKey:@"LastLoginTime"] longLongValue]];
                    if([ddata objectForKey:@"Sex"]==[NSNull null])
                    {
                         ouser.sex=1;
                    }else
                    {
                     ouser.sex=[[ddata objectForKey:@"Sex"] intValue];
                    }
                   
                    ouser.isStr=[ddata objectForKey:@"Sign"];
                    ouser.userId=[[ddata objectForKey:@"userId"] intValue];
                    ouser.vip=[[ddata objectForKey:@"VIP"] intValue];
                    ouser.locationX=[[ddata objectForKey:@"coordX"] doubleValue];
                    ouser.locationY=[[ddata objectForKey:@"coordY"] doubleValue];
                    ouser.userHeadImage=[path stringByAppendingString:[ddata objectForKey:@"Avatar"]];
                    [dataarr addObject:ouser];
                }
                
                
            }
            [self.table reloadData];
        }
    }];
}

@end
